import { NotifyModel } from '@/models/notify'
import NotifyTable from '@/pages/Notify/components/NotifyTable'
import NotifyUpdateModal from '@/pages/Notify/components/NotifyUpdateModal'
import { NotifyVO, saveNotify, updateNotify } from '@/services/NotifyService'
import { useSnapshot } from '@@/exports'
import { PageContainer } from '@ant-design/pro-components'
import { ActionType } from '@ant-design/pro-table'
import { message } from 'antd'
import { useRef } from 'react'

const Notify = () => {
  // 表格引用
  const tableRef = useRef<ActionType>()
  let notifyState = useSnapshot(NotifyModel.state)
  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let dto: NotifyVO = JSON.parse(JSON.stringify(formData))
    // 字段处理

    let func = notifyState.notify ? updateNotify : saveNotify
    if (await func(dto)) {
      message.success('操作成功')
      // 刷新表格
      tableRef.current?.reload()
      return true
    }
    return false
  }

  return (
    <PageContainer title="通知">
      {/*通知表格 开始*/}
      <NotifyTable tableRef={tableRef} />
      {/*通知表格 结束*/}

      {/*修改模态框 开始*/}
      <NotifyUpdateModal onFinish={onUpdateFinish} />
      {/*修改模态框 结束*/}
    </PageContainer>
  )
}

export default Notify
